<?php
class TemplateHelper {
    private static $layoutName = "searchscreen";

    public static function getDefaultPageHeader() {
        global $smarty;
        global $stat;

        $smarty->assign("stat", $stat);
        return $smarty->fetch("layout/header.tpl");
    }
    
    public static function getDefaultPageFooter($params = array()) {
        global $smarty;
        if (count($params) > 0) {
            $smarty->assign("type", $params['type']);
        }
        return $smarty->fetch("layout/footer.tpl");
    }

    public static function addCaptchaService() {
        $captchas = getCaptchaObject();
        $result = "<input type='hidden' name='random' value='" . $captchas->random() . "'>";
        $result .= "Please type in the security letters<br><input type='text' name='captcha' style='width:200px;'><br>";
        $result .= $captchas->image ();
        $result .= "<br><a href=\"javascript:captchas_image_reload('captchas.net')\">Reload Image</a>";
        return $result;
    }

    public static function redirectPage($page, $subpage, $params) {
        $url = "index.php?choice=$page";
        if ($subpage != "") { $url .= "&subpage=$subpage"; }
        if (count($params) > 0) { $url .= "&params=" . urlencode(serialize($params)); }
        echo "<meta http-equiv='refresh' content='0;url=$url'>";
    }

    public static function getTemplateFile($pageName, $templateFileName, $isAdmin = false) {
        if ($isAdmin) {
            return "administrator/pages/$pageName/$templateFileName.tpl";
        } else {
            return "pages/$pageName/$templateFileName.tpl";
        }
    }

    public static function useLayout($layoutName, $isAdmin = false) {
        $fileName = "site/layout/$layoutName/layout.$layoutName.tpl";
        if ($isAdmin) { $fileName = "site/administrator/layout/$layoutName/layout.$layoutName.tpl"; }
        if (file_exists($fileName)) {
            self::$layoutName = $layoutName;
        } else {
            self::$layoutName = "searchscreen";
        }
    }

    public static function addButton($title, $fn) {
        return array(
            title => $title,
            action => $fn
        );
    }

    public static function getLayout() {
        return self::$layoutName;
    }
    
    public static function createDynamicTableArray($count, $values = array()) {
        $result = array();
        $c = max($count, count($values));
        for ($i = 0; $i < $c; $i++) {
            if (count($values) > $i) {
                $result[] = $values[$i];
            }
            $result[$i]['rownum'] = ($i+1);
        }
        return $result;
    }
}

?>
